package moe.service;

import java.util.Map;

import moe.config.Configuration;
import moe.config.Constants;
import moe.config.PropertiesUtil;
import moe.util.JSONBuilder;

import org.apache.http.HttpResponse;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

public class CommonHandler extends AbstractHandler {
	public void config(HttpContext context, Map<String, String[]> query) {
		HttpResponse response = (HttpResponse) context.getAttribute(ExecutionContext.HTTP_RESPONSE);

		JSONBuilder builder = JSONBuilder.build().callback(getCallback(query));
		for (String field : PropertiesUtil.getPropertyKeys()) {
			if (Configuration.isPrivacy(field)) {
				continue;
			}
			String value = PropertiesUtil.getProperty(field);
			if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
				builder.add(field, Boolean.parseBoolean(value));
			} else if (value.matches("\\d+")) {
				try {
					builder.add(field, Long.valueOf(value));
				} catch (NumberFormatException e) {
					builder.add(field, value);
				}
			} else {
				builder.add(field, value);
			}
		}
		if (PropertiesUtil.hasProperty(Constants.COLLECTION_LIBRARY)) {
			builder.add("hasCollection", true);
		}

		sendJsonResponse(response, builder.buildString());
	}
}
